package cn.wh.org.controller;

import cn.wh.basic.util.AjaxResult;
import cn.wh.basic.util.PageList;
import cn.wh.org.domain.Employee;
import cn.wh.org.query.EmployeeQuery;
import cn.wh.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")

public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    @GetMapping("/{id}")

    public Employee queryById(@PathVariable Long id){
        return employeeService.queryById(id);
    }

    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> queryEmp(@PathVariable("shopId") Long shopId){
        return employeeService.queryEmp(shopId);
    }

    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");

        }

    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id){
        try {
            employeeService.removeById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");
        }
    }

    @DeleteMapping
    public AjaxResult removeAll(@RequestBody List<Long> ids){
        try {
            employeeService.deleteAll(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");
        }
    }


}
